package utils

import (
	"io"
	"log"
	"net/http"
	"strconv"
)

type Weber interface {
	GetAddr(ip string) string
}

type Web struct {
	ip   string
	addr string
}

func GetBodyFormatJson(w http.ResponseWriter, r *http.Request) ([]byte, error) {
	bytes, err := io.ReadAll(r.Body)
	return bytes, err
}

func GetParamsFormatJson(w http.ResponseWriter, r *http.Request) map[string][]string {
	return r.URL.Query()
}

func GetParamsFormatMap(w http.ResponseWriter, r *http.Request) map[string]any {
	//map[string][]string
	defer func() {
		if e := recover(); e != nil {
			log.Println(e)
		}
	}()
	mapData := make(map[string]any)
	query := r.URL.Query()
	for k, v := range query {
		if len(v) == 1 {
			if v[0] == "true" || v[0] == "false" {
				boolValue, err := strconv.ParseBool(v[0])
				if err != nil {
					panic(err)
				}
				mapData[k] = boolValue
			}
		}
		mapData[k] = v
	}
	return mapData
}

func (Web) GetAddr(ip string) string {
	return ip
}
